package com.woniu.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.woniu.job.entity.Company;
import com.woniu.job.entity.User;
import com.woniu.job.entity.Work;
import com.woniu.job.exception.service.AccountExistException;
import com.woniu.job.exception.service.ServiceException;
import com.woniu.job.mapper.CompanyMapper;
import com.woniu.job.mapper.UserMapper;
import com.woniu.job.mapper.WorkMapper;
import com.woniu.job.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 *  状态：1、账户正常，2、未激活（必须完成公司信息填写与营业执照上传）3、失信企业
 * 状态（面试表）
 * </p>
 *
 * @author song
 * @since 2019-12-24
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private WorkMapper workMapper;

    /**
     * @Description:用户注册
     * @Param:
     * @return:
     * @Author: 福尔摩司
     * @Date: 2019/12/24 22:25
     */
    @Override
    public void register(Company company) {
        String id = UUID.randomUUID().toString().replace("-","");
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("account", company.getAccount());
            int count = companyMapper.selectCount(queryWrapper);
            if (count != 0) {
                throw new ServiceException(new AccountExistException("用户名重复"));
            }
            company.setId(id);
            company.setStatus("2");
            companyMapper.insert(company);
            // company.setPassword(new SimpleHash("MD5",user.getPassword(),user.getAccount(),10).toString());

        } catch (DataAccessException e) {
            throw new ServiceException(e);
        }

    }
/**
* @Description:用户登录
* @Param:
* @return:
* @Author: 福尔摩司
* @Date: 2019/12/26 12:00
*/
    @Override
    public Company getByAccount(String account) {
        if(StringUtils.isEmpty(account)){
            return  null;
        }
        LambdaQueryWrapper<Company> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getAccount,account);
        List<Company> list=list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            //用户名是唯一的,所以list的大小为1
            return list.get(0);
        }
        return null;
    }
/**
* @Description:搜索职位
* @Param:
* @return:
* @Author: 福尔摩司
* @Date: 2019/12/26 12:01
*/
    @Override
    public List<Work> select(Work work) {
        /*QueryWrapper<Work> queryWrapper =new QueryWrapper<>();
        queryWrapper.like("name",work.getName());
        List<Work> works =workMapper.selectList(queryWrapper);*/

        List<Work> works =companyMapper.getAll(work.getName());
        return works;
    }

    @Override
    public void addMessage(Company company) {
        companyMapper.updateById(company);
    }

    /**
     * 修改企业信息
     * @param company
     */
    @Override
    public void update(Company company) {
        companyMapper.updateById(company);
    }



    @Override
    public Company getCompanysById(String id) {
        return companyMapper.getCompanysById(id);
    }
}
